var searchResults;
var iWait = 0;
var srchString = '';

$(document).ready(function() {
	// Instant Search
	$("#searchText").keyup(function(event) {
		event.preventDefault();
		if(iWait>0){
		clearTimeout(iWait);
		iWait = 0;
		}
		if(srchString != $("#searchText").val().trim()){
		srchString = $("#searchText").val().trim();
		// Wait for pause before searching
		if(srchString.length >=3)
		iWait = setTimeout(ajaxquery(),1000);
		}
	});
	// On Click
	$("#search-button").click(function(event) {
		event.preventDefault();
		ajaxquery();
	});
	
});
function ajaxquery(){
	var passData;
	var catval = $("input[name='catlist[]']:checkbox:checked").map(function(){
	return this.value;
	}).get();
	var distval = $("input[name='distlist[]']:checkbox:checked").map(function(){
	return this.value;
	}).get();
	
	var cat = catval.join(",");
	var dist = distval.join(",");
	//alert(cat+' => '+dist);
	if(cat == '0' && dist == '0' && srchString.length < 2){
		useData("Invalid Search!");
		return false;
	}
	if(srchString.length < 2){
		passData = "searchText=&mycat="+encodeURI(cat)+"&mydist="+encodeURI(dist);
	}
	else{
		passData = "searchText="+encodeURI(srchString)+"&mycat="+encodeURI(cat)+"&mydist="+encodeURI(dist);
	}
	//test
	//passData = "searchText=nep&catt=5&distt=0";
	//alert(passData);
	// SEARCH
	$.ajax({
	  //dataType:"json",
	  type: "POST",
	  data: passData,
	  url: "/test/wp-content/themes/simplest/search.php",
	  //context: document.body,
	  beforeSend: function(){jQuery("#ajaxloading").show();},
	  complete: function(){jQuery("#ajaxloading").hide();}
	}).done(function(response, textStatus, jqXHR){
		useData(response);
		return false;
		//markerPlot(data);
	}).error(function(jqXHR, textStatus, errorThrown) {
		useData("!Error!");
		return false;
	});
}

